import 'package:flutter/material.dart';
import 'package:s_mankey/config/storage_manager.dart';
import 'package:s_mankey/generated/I18n.dart';

class LocaleVM extends ChangeNotifier {
  ///语言持久化键值.
  static const keyIsCN = 'KeyIsCN';

  ///第1个为简繁体: true为简体,第2是推送通知: ture为推送(暂未做处理,因为还没有这个功能), 第3和第4个样式不一样暂时没用.
  List<bool> _openValueList = [false, false, null, null];

  List<bool> get openValueList => _openValueList;

  ///从本地取出保存的字体并在状态管理初始化.
  LocaleVM() {
    _openValueList[0] =
        StorageManager.sharedPreferences.getBool(keyIsCN) ?? false;
  }

  Locale get locales {
    //如果是null则跟随系统语言,,因为只有两种语言, 则直接写死了.
    return _openValueList[0] ? Locale('zh', 'CN') : Locale('zh', 'TW');
  }

  ///前端打开开关后渲染界面,并持久化数据.
  switchLocale({bool isOpen, int index}) {
    index == 0
        ? _openValueList[index] = isOpen
        : _openValueList[index] = isOpen;
    notifyListeners();
    StorageManager.sharedPreferences.setBool(keyIsCN, isOpen);
  }

  static String localeName(index, context) {
    switch (index) {
      case 0:
        return I18n.current.font;
      case 1:
        return I18n.current.pushNotification;
      case 2:
        return I18n.current.selectCity;
      case 3:
        return I18n.current.version;
      default:
        return '';
    }
  }
}
